﻿Public Partial Class FrmTariff_Popup
    Inherits System.Web.UI.Page

    Dim clsGet As New ClassFunctionGetData
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            ViewState("NotMAT") = "''"
            Try
                Dim ds As DataSet = Session("MAT_USE")
                For Each dr As DataRow In ds.Tables(0).Rows
                    ViewState("NotMAT") &= ",'" & dr.Item("Material") & "'"
                Next
            Catch ex As Exception

            End Try
            'Dim lb As Label = CType(Master.FindControl("lbMenu"), Label)
            'lb.Text = "Tariff"
            Dim Plant As String = ""
            If Not Session("PORT_PRODUCT") Is Nothing Then
                If (Session("PORT_PRODUCT") = "L") Then
                    Plant = "1103"
                Else
                    Plant = "1104"
                End If
            End If
            ViewState("Plant") = Plant
            LoadTariff("", Plant)
            ViewState("UseMAT") = CreateDataset()
            gridMatuse.DataSource = ViewState("UseMAT")
            gridMatuse.DataBind()
        End If
    End Sub

    Private Sub LoadTariff(ByVal Mat As String, ByVal Plant As String)
        ViewState("Tariff") = clsGet.Get_MeterailDefault(Mat, False, ViewState("NotMAT").ToString(), "", "", "", "", "", "", "", Plant)
        gridTariff.PageIndex = 0
        gridTariff.DataSource = ViewState("Tariff")
        gridTariff.DataBind()
    End Sub
    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " parent.closeModal('1');", True)
    End Sub

    Public Function CreateDataset() As DataSet
        Dim ds As New DataSet
        Dim dt As New DataTable
        Dim dr As DataRow
        ds.Tables.Add(dt)
        dt.Columns.Add("MATERIAL", GetType(String))
        dt.Columns.Add("Text_line", GetType(String))
        dt.Columns.Add("Unit", GetType(String))
        dt.Columns.Add("Count", GetType(String))
        dt.Columns.Add("MAT_Default", GetType(String))
        dt.Columns.Add("KBETR", GetType(Decimal))
        dt.Columns.Add("KONWA", GetType(String))
        dt.Columns.Add("MAT_Relation", GetType(String))
        dt.Columns.Add("MAT_ComputeM", GetType(String))
        dr = ds.Tables(0).NewRow
        dr("MATERIAL") = ""
        dr("Text_line") = ""
        dr("Unit") = ""
        dr("Count") = ""
        dr("MAT_Default") = ""
        dr("KBETR") = "0.0"
        dr("KONWA") = ""
        dr("MAT_Relation") = ""
        dr("MAT_ComputeM") = ""
        ds.Tables(0).Rows.Add(dr)
        ds.Tables(0).AcceptChanges()
        Return ds
    End Function

    Public Sub Paginate(ByVal sender As Object, ByVal e As CommandEventArgs)
        Dim CurrentIndex As Integer = gridTariff.PageIndex
        Select Case e.CommandArgument.ToString().ToLower()
            Case "first"
                gridTariff.PageIndex = 0
            Case "prev"
                If (gridTariff.PageIndex <> 0) Then
                    gridTariff.PageIndex = CurrentIndex - 1
                End If
            Case "next"
                If (gridTariff.PageIndex <> gridTariff.PageCount) Then
                    gridTariff.PageIndex = CurrentIndex + 1
                End If
            Case "last"
                gridTariff.PageIndex = gridTariff.PageCount
        End Select
        gridTariff.DataSource = ViewState("Tariff")
        gridTariff.DataBind()
    End Sub

    Protected Sub ddlPage_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim Page As GridViewRow = gridTariff.BottomPagerRow
        Dim ddlPage As DropDownList = CType(Page.Cells(0).FindControl("ddlPage"), DropDownList)
        gridTariff.PageIndex = ddlPage.SelectedIndex
        gridTariff.DataSource = ViewState("Tariff")
        gridTariff.DataBind()
    End Sub

    Private Sub gridTariff_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gridTariff.RowCommand
        Dim Index As Integer = Convert.ToInt32(e.CommandArgument)
        Dim IndexDelete As Integer = Index
        If (gridTariff.PageIndex <> 0) Then
            Index = Index Mod (gridTariff.PageIndex * 10)
        End If
        If (e.CommandName = "cmdadd") Then
            ViewState("NotMAT") &= ",'" & gridTariff.Rows(Index).Cells(0).Text & "'"
            LoadDataCountMAtUSe()
            Dim Useds As DataSet = ViewState("UseMAT")
            If (Useds.Tables(0).Rows(0).Item("MATERIAL") & "" = "") Then
                'กรณี แถวแรก
                Useds.Tables(0).Rows(0).Item("MATERIAL") = gridTariff.Rows(Index).Cells(0).Text
                Useds.Tables(0).Rows(0).Item("Text_line") = gridTariff.Rows(Index).Cells(1).Text
                Useds.Tables(0).Rows(0).Item("Unit") = gridTariff.Rows(Index).Cells(2).Text
                Useds.Tables(0).Rows(0).Item("Count") = "1"
                Useds.Tables(0).Rows(0).Item("MAT_Default") = "N"
                Useds.Tables(0).Rows(0).Item("KBETR") = gridTariff.DataKeys(Index).Values("KBETR")
                Useds.Tables(0).Rows(0).Item("KONWA") = gridTariff.DataKeys(Index).Values("KONWA")
                Useds.Tables(0).Rows(0).Item("MAT_Relation") = gridTariff.DataKeys(Index).Values("MAT_Relation") & ""
                Useds.Tables(0).Rows(0).Item("MAT_ComputeM") = "Y"
                Useds.AcceptChanges()
            Else
                'กรณีแถวสอง
                Dim dr As DataRow = Useds.Tables(0).NewRow()
                dr.Item("MATERIAL") = gridTariff.Rows(Index).Cells(0).Text
                dr.Item("Text_line") = gridTariff.Rows(Index).Cells(1).Text
                dr.Item("Unit") = gridTariff.Rows(Index).Cells(2).Text
                dr.Item("Count") = "1"
                dr.Item("MAT_Default") = "N"
                dr.Item("KBETR") = gridTariff.DataKeys(Index).Values("KBETR")
                dr.Item("KONWA") = gridTariff.DataKeys(Index).Values("KONWA")
                dr.Item("MAT_Relation") = gridTariff.DataKeys(Index).Values("Mat_Relation") & ""
                dr.Item("MAT_ComputeM") = "Y"
                Useds.Tables(0).Rows.Add(dr)
                Useds.AcceptChanges()
            End If
            ViewState("UseMAT") = Useds
            Dim dsDelete As DataSet = ViewState("Tariff")
            dsDelete.Tables(0).Rows(IndexDelete).Delete()
            dsDelete.AcceptChanges()
            ViewState("Tariff") = dsDelete
            gridTariff.DataSource = ViewState("Tariff")
            gridTariff.DataBind()
            gridMatuse.DataSource = ViewState("UseMAT")
            gridMatuse.DataBind()
        End If
    End Sub

    Private Sub LoadDataCountMAtUSe()
        Dim ds As DataSet = ViewState("UseMAT")
        For i = 0 To gridMatuse.Rows.Count - 1
            Dim txt As TextBox = CType(gridMatuse.Rows(i).FindControl("txtCount"), TextBox)
            ds.Tables(0).Rows(i).Item("Count") = txt.Text
            ds.AcceptChanges()
        Next
        ViewState("UseMAT") = ds
    End Sub

    Private Sub gridTariff_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridTariff.RowDataBound
        If (e.Row.RowType = DataControlRowType.Pager) Then
            Dim Page As GridViewRow = gridTariff.BottomPagerRow
            Dim ddlPage As DropDownList = CType(e.Row.FindControl("ddlPage"), DropDownList)
            Dim lblPageCount As Label = CType(e.Row.FindControl("lbPageCount"), Label)
            If Not (ddlPage Is Nothing) Then
                For i As Integer = 0 To gridTariff.PageCount - 1
                    Dim intPageNumber As Integer = i + 1
                    Dim IsItem As New ListItem(intPageNumber.ToString())
                    If (i = gridTariff.PageIndex) Then
                        IsItem.Selected = True
                    End If
                    ddlPage.Items.Add(IsItem)
                Next
            End If
            If Not (lblPageCount Is Nothing) Then
                lblPageCount.Text = gridTariff.PageCount.ToString()
            End If
        End If
    End Sub

    Protected Sub btnSearchTariff_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSearchTariff.Click
        LoadTariff(txtTariff.Text, ViewState("Plant"))
    End Sub

    Private Sub gridMatuse_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridMatuse.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim txt As TextBox = CType(e.Row.FindControl("txtCount"), TextBox)
            If (gridMatuse.DataKeys(e.Row.RowIndex).Values("MAT_Default") & "" = "") Then
                txt.Visible = False
                e.Row.Cells(0).Text = ""
            End If
        End If
    End Sub

    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click
        Dim dsTariff As DataSet
        Try
            dsTariff = Session("MAT_USE")
        Catch ex As Exception
            dsTariff = CreateDataset()
            dsTariff.Tables(0).Rows(0).Delete()
            dsTariff.AcceptChanges()
        End Try

        LoadDataCountMAtUSe()
        If Not (dsTariff Is Nothing) Then
            dsTariff.Tables(0).Merge(CType(ViewState("UseMAT"), DataSet).Tables(0))
            dsTariff.AcceptChanges()
            Session("MAT_USE") = dsTariff
        Else
            Session("MAT_USE") = ViewState("UseMAT")
        End If
        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " parent.closeModal('3');", True)
    End Sub

    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "")
        End If
    End Sub
End Class